﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MethodBox.Interface
{
    class Login
    {
        static void Getter(string un,string pw)
        { 
            //string result = RequestData(un,"http://4ptcifn7j9.258jz.cn/api/login.php",$"username={un}&password={pw}",new List<string> { "A0"});
            //Console.WriteLine(result + "\n");

            //for (int i = 0; i < 200; i++)
            //{
            //    string key = ProduceVerifyKey();
            //    Console.ReadKey();
            //    Console.WriteLine("请求码：" + key + ".");

            //    StreamWriter sw = new StreamWriter("C:\\users\\administrator\\desktop\\code.txt",true);
            //    sw.Write("请求码：" + key + "\n");
            //    sw.Close();
            //}

            Console.ReadKey();
        }

        /// <summary>
        /// 向MethodBox提交数据请求
        /// </summary>
        /// <param name="account">用户名</param>
        /// <param name="POSTURL">请求提交的地址</param>
        /// <param name="PostData">提交的数据(字符串)</param>
        /// <param name="NeedType">需要的许可证类型</param>
        /// <returns></returns>
        public static string RequestData(string account,string POSTURL, string PostData,List<string> NeedType,bool apt_api)
        {
            //发送请求的数据
            WebRequest myHttpWebRequest = WebRequest.Create(POSTURL);
            myHttpWebRequest.Method = "POST";
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byte1 = encoding.GetBytes(PostData);
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = byte1.Length;
            Stream newStream = myHttpWebRequest.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            newStream.Close();

            //发送成功后接收返回的XML信息
            HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); string lcHtml = string.Empty;
            Encoding enc = Encoding.GetEncoding("UTF-8");
            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream, enc);
            lcHtml = streamReader.ReadToEnd();
            if (apt_api)
            {
                if (lcHtml.StartsWith("您")) { return lcHtml; }
                if (NeedType.Contains(lcHtml)) { return $"您好，{account}。"; }
                else { return $"您的许可证类型为{lcHtml}。\n使用此应用需要的许可证类型与您的许可证不符，请前往获取能够使用的许可证。"; }
            }
            else
            {
                return lcHtml;
            }
        }

        public static string ProduceVerifyKey()
        {
            Random rd = new Random();

            int rd1 = rd.Next(0, 10);
            int rd2 = rd.Next(0, 10);
            int rd3 = rd.Next(0, 10);
            int rd4 = rd.Next(0, 10);
            int rd5 = rd.Next(0, 10);

            while (!(rd1 + rd2 + rd3 >= 15 && rd1 + rd2 + rd4 <= 20 && rd1 % 3 == 0 && (rd2 + rd5) % 3 == 1 && rd5 + rd3 == 5 && (rd1 != rd2) && (rd2 != rd3) && (rd4 != rd5) && rd3 <= 5))
            {
                rd1 = rd.Next(0, 10);
                rd2 = rd.Next(0, 10);
                rd3 = rd.Next(0, 10);
                rd4 = rd.Next(0, 10);
                rd5 = rd.Next(0, 10);
            }

            //产生字母
            List<string> zmb = new List<string>();

            for (int i = 0; i < 10; i++)
            {
                int rdx = rd.Next(1, 10);

                switch (rdx)
                {
                    case 1:
                        zmb.Add("A");
                        break;

                    case 2:
                        zmb.Add("B");
                        break;

                    case 3:
                        zmb.Add("C");
                        break;

                    case 4:
                        zmb.Add("D");
                        break;

                    case 5:
                        zmb.Add("E");
                        break;

                    case 6:
                        zmb.Add("F");
                        break;

                    case 7:
                        zmb.Add("G");
                        break;

                    case 8:
                        zmb.Add("H");
                        break;

                    case 9:
                        zmb.Add("I");
                        break;

                    default:
                        break;
                }
            }

            return $"{zmb[0]}{zmb[7]}{rd2}{zmb[2]}-{rd3}{rd1}{zmb[3]}{rd5}-{zmb[3]}{rd5}{rd1}{zmb[5]}-{zmb[6]}K{rd4}1-{zmb[0]}{rd2}{zmb[8]}{rd4}";

        }

    }
}
